package foundation.pcmUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.jdom2.Element;
import org.jdom2.JDOMException;

import foundation.fileUtil.XMLFileUtil;

public class RepositoryModelHelper {
	// 库模型文件名
	private String repositoryFileName;
	// 操纵xml文件的帮助类对象
	private XMLFileUtil xmlHelper;

	public RepositoryModelHelper() {

	}

	public RepositoryModelHelper(String repositoryFileName) {
		this.repositoryFileName = repositoryFileName;
		File repositoryFile = new File(repositoryFileName);
		xmlHelper = new XMLFileUtil(repositoryFile);

	}
	
	public List<Element> readNode(String pathName) throws JDOMException{
		return xmlHelper.readNode(pathName);
	}
	public List<Element> readNode(String pathName,String prefix,String url) throws JDOMException {
		return xmlHelper.readNode(pathName, prefix, url);
	}
	
	public void write() throws FileNotFoundException, IOException{
		xmlHelper.write(repositoryFileName);
	}
	
	public void close(){
		xmlHelper.close(repositoryFileName);
	}
	/**
	 * 在repository模型中，根据id号获取组件名
	 * @param compID
	 * @return
	 */
	public String getCompNameByID(String compID){	
		String path="/repository:Repository/components__Repository[@id='"+compID+"']";
		String compName = null;
		Element compNameEle;
		try {
			compNameEle = xmlHelper.readNode(path,"repository",
					"http://palladiosimulator.org/PalladioComponentModel/Repository/5.1").get(0);			
			compName = compNameEle.getAttributeValue("entityName");			
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return compName;	
	}
	/**
	 * 根据组件名获取ID号
	 * @param compName
	 * @return
	 */
	public String getIDByCompName(String compName){	
		String path="/repository:Repository/components__Repository[@entityName='"+compName+"']";
		String compID = null;
		Element compIDEle;
		try {
			compIDEle = xmlHelper.readNode(path,"repository",
					"http://palladiosimulator.org/PalladioComponentModel/Repository/5.1").get(0);			
			compID = compIDEle.getAttributeValue("id");			
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return compID;	
	}
	
	/**
	 * 计算某个组件的资源需求
	 * @param compName
	 * @return
	 */
	public Float getResDemand(String compName) {
		float resDemand = 0f;
		float sumResDemand = 0f;
		String path = "/repository:Repository/components__Repository[@entityName='"
				+ compName
				+ "']/"
				+ "serviceEffectSpecifications__BasicComponent";
		try {
			List<Element> seffs = xmlHelper
					.readNode(path, "repository",
							"http://palladiosimulator.org/PalladioComponentModel/Repository/5.1");

			for (int i = 0; i < seffs.size(); i++) {
				List<Element> behaviours = seffs.get(i).getChildren();
				for (int j = 0; j < behaviours.size(); j++) {
					if (behaviours.get(j).getChildren().size() > 0) {
						Element resDemandEle = behaviours.get(j).getChildren()
								.get(0).getChildren().get(0);
						if (resDemandEle
								.toString()
								.equals("[Element: <specification_ParametericResourceDemand/>]")) {
							resDemand = Float.valueOf(resDemandEle
									.getAttributeValue("specification"));
							sumResDemand+=resDemand;							
						}
					}
				}
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sumResDemand;
	}
	
	public String getRepositoryFileName() {
		return repositoryFileName;
	}

	public void setRepositoryFileName(String repositoryFileName) {
		this.repositoryFileName = repositoryFileName;
	}

	public XMLFileUtil getXmlHelper() {
		return xmlHelper;
	}

	public void setXmlHelper(XMLFileUtil xmlHelper) {
		this.xmlHelper = xmlHelper;
	}

}



